﻿using System.Threading.Tasks;
using Grpc.Core;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    public class RpcUserServiceRealize : RpcUserService.RpcUserServiceBase
    {
        private readonly IRepository<User, int> _userRepository;

        public RpcUserServiceRealize(
            IRepository<User, int> userRepository)
        {
            _userRepository = userRepository;
        }

        // 获取
        public override async Task<RpcUserResponse> GetAsync(RpcUserRequest request, ServerCallContext context)
        {
            var userId = int.Parse(request.Id);
            var user = await _userRepository.GetAsync(userId);
            var rpcUser = new RpcUserResponse()
            {
                User = new RpcUser()
                {
                    UserId = user.Id.ToString(),
                    UserName = user.UserName ?? string.Empty,
                    Avatar = user.Avatar ?? string.Empty,
                    NickName = user.NickName ?? string.Empty,
                    Sex = user.Sex ?? string.Empty,
                    Phonenumber = user.Phonenumber ?? string.Empty,
                    Email = user.Email ?? string.Empty,
                    IdCard = user.IdCard ?? string.Empty,
                    Balance = user.Balance.ToString(),
                    Score = user.Score.ToString()
                }
            };
            return rpcUser;
        }
    }
}